<!-- 14课程评价-2用户-13课程 -->
<template>
    <div class="about">
        <el-form :inline="true" :v-model="queryInfo" class="demo-form-inline">
            <el-form-item label="用户昵称">
                <el-input v-model="queryInfo.nickName" placeholder="请输入关键字" clearable />
            </el-form-item>
            <el-form-item label="课程名称">
                <el-input v-model="queryInfo.courseName" placeholder="请输入关键字" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="getList">搜索</el-button>
            </el-form-item>
        </el-form>
        <!-- <div class="mb20">
            <el-button @click="onAdd" type="primary" icon="Plus" plain>新增</el-button>
        </div> -->
        <el-table :data="tableData" style="width: 100%">
            <el-table-column type="selection" width="50" />
            <el-table-column fixed prop="id" label="编号" width="100" /> <el-table-column prop="content" label="评论内容"
                width="150" />
            <el-table-column prop="grade" label="评价分数" width="200">
                <template #default="scope">
                    <el-rate v-model="scope.row.grade" :texts="['1', '2', '3', '4', '5']" show-text />
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="评价时间" width="180" />
            <el-table-column prop="user.nickName" label="用户昵称" width="120" />
            <el-table-column prop="course.courseName" label="课程名称" width="120" />
            <el-table-column fixed="right" label="操作" min-width="150">
                <template #default="scope">
                    <el-button link type="primary" size="small" @click="onEdit(scope.row)">修改</el-button>
                    <el-button link type="primary" @click="onDel(scope.row.id)" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>

    <el-pagination style="margin-top: 10px;" v-model:current-page="queryInfo.pageNum"
        v-model:page-size="queryInfo.pageSize" :page-sizes="[1, 5, 10, 20]" size="default" background="false"
        layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />

    <!-- 添加，编辑弹框 -->
    <el-dialog v-model="saveOrUpdateVisible" :title="saveOrUpdateTitle" width="500" @close="close()">
        <el-form :model="coursemark">
            <el-form-item label="评论内容">
                <el-input v-model="coursemark.content" autocomplete="off" />
            </el-form-item>

            <el-form-item label="评价分数">
                <el-rate v-model="coursemark.grade" :texts="['1', '2', '3', '4', '5']" show-text />
            </el-form-item>

            <el-form-item label="评价时间">
                <el-date-picker v-model="coursemark.createTime" type="date" />
            </el-form-item>

            <el-form-item label="用户昵称：">
                {{ coursemark.user.nickName }}
            </el-form-item>

            <el-form-item label="课程名称：">
                {{ coursemark.course.courseName }}
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="close">
                    取消
                </el-button>
                <el-button type="primary" @click="saveOrUpdate">
                    确认
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
//引入
import { onMounted, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

//弹出框
const saveOrUpdateVisible = ref(false);
const saveOrUpdateTitle = ref('修改');
const coursemark = ref({
    id: 0,
    content: '',
    grade: 0,
    createTime: '',
    user: {},
    course: {}
})
//修改的页面
const onEdit = (row) => {
    saveOrUpdateVisible.value = true;
    coursemark.value = {
        id: row.id,
        content: row.content,
        grade: ensureValidGrade(Number(row.grade)),
        createTime: row.createTime,
        user: row.user,
        course: row.course
    };
}
// 确保评分值在 1 - 5 之间
const ensureValidGrade = (grade) => {
    return Math.min(Math.max(grade, 1), 5);
};
//添加的页面
// const onAdd = () => {
//     saveOrUpdateTitle.value = '添加';
//     saveOrUpdateVisible.value = true;
//     coursemark.value = {
//         id: 0,
//         content: '',
//         grade: 0,
//         createTime: '',
//     }
// }
//取消
const close = () => {
    saveOrUpdateVisible.value = false;
    coursemark.value = {
        id: 0,
        content: '',
        grade: 0,
        createTime: '',
        user: {},
        course: {}
    }
}
//添加与修改
const saveOrUpdate = () => {
    // if (saveOrUpdateTitle.value == '添加') {
    // axios.post("/coursemark/addCoursemark", coursemark.value)
    //     .then(resp => {
    //         console.log(resp.data)
    //         let result = resp.data;
    //         if (result.code == 1) {
    //             getList();
    //             ElMessage({
    //                 message: '添加成功！',
    //                 type: 'success',
    //             })
    //         } else {
    //             ElMessage({
    //                 message: '添加失败！',
    //                 type: 'error',
    //             })
    //         }
    //     })
    // } else {

    // }
    axios.put("/coursemark/updateCoursemark", {
        id: coursemark.value.id,
        content: coursemark.value.content,
        grade: ensureValidGrade(Number(coursemark.value.grade)),
        createTime: coursemark.value.createTime,
        user: coursemark.value.user,
        course: coursemark.value.course
    })
        .then(resp => {
            console.log(resp.data)
            let result = resp.data;
            if (result.code == 1) {
                getList();
                ElMessage({
                    message: '修改成功！',
                    type: 'success',
                })
            } else {
                ElMessage({
                    message: '修改失败！',
                    type: 'error',
                })
            }
        })
    saveOrUpdateVisible.value = false;
    coursemark.value = {
        id: 0,
        content: '',
        grade: 0,
        createTime: '',
        user: {},
        course: {}
    }
}
// 删除
const onDel = id => {
    ElMessageBox.confirm(
        '真的要删除吗？',
        '提示',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            axios.delete("/coursemark/deleteCoursemark?id=" + id)
                .then(resp => {
                    console.log(resp.data)
                    let result = resp.data;
                    if (result.code == 1) {
                        getList();
                        ElMessage({
                            message: '删除成功！',
                            type: 'success',
                        })
                    } else {
                        ElMessage({
                            message: '删除失败！',
                            type: 'error',
                        })
                    }
                })
        })
        .catch(() => { })
}
onMounted(() => {
    console.log('onMounted')
    getList();
})
const queryInfo = ref({
    pageNum: 1,
    pageSize: 5,
    id: '',
    content: '',
    grade: '',
    createTime: '',
    nickName: '',
    courseName: '',
})
const tableData = ref([]);
const total = ref(0);
//获取列表
const getList = () => {
    axios.get("/coursemark/selectCoursemark",
        {
            params: queryInfo.value
        })
        .then(resp => {
            console.log(resp.data.data.list)
            if (resp.data.code == 1) {
                total.value = resp.data.data.total
                tableData.value = resp.data.data.list
            } else {
                ElMessage({
                    message: "查询失败！",
                    type: 'error',
                })
            }
        })
}
//分页
const handleSizeChange = (val) => {
    queryInfo.value.pageSize = val;
    queryInfo.value.pageNum = 1;
    getList();
}
const handleCurrentChange = (val) => {
    queryInfo.value.pageNum = val;
    getList();
}
</script>

<style scoped></style>